package com.gzzhcm.czh.myceshi;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.math.BigDecimal;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = "MainActivity";
    private String accessKeyId = "LTAIg2JpHqFb447F";
    private String accessKeySecret = "xWnN2ONZvJ0yWzLmlIlNAb7LEcpiSj";
    private String endpoint ="oss-cn-beijing.aliyuncs.com";
    private String bucketName = "czhxiaqu";
    private String lujin = "https://czhxiaqu.oss-cn-beijing.aliyuncs.com/";
    // 相册选择回传吗
    public final static int GALLERY_REQUEST_CODE = 1;
    private  Button button,btLuzi,btfragent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        JCVideoPlayerStandard jcVideoPlayerStandard = (JCVideoPlayerStandard) findViewById(R.id.videoplayer);
        jcVideoPlayerStandard.setUp("http://2449.vod.myqcloud.com/2449_22ca37a6ea9011e5acaaf51d105342e3.f20.mp4",
                JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, "嫂子闭眼睛");
//        jcVideoPlayerStandard.thumbImageView.setImage("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640");
                intView();
    }

    private void intView() {
         button = findViewById(R.id.bt_shangchuang);
         btLuzi = findViewById(R.id.bt_lusp);
        btfragent = findViewById(R.id.bt_fragment);
        button.setOnClickListener(this);
        btLuzi.setOnClickListener(this);
        btfragent.setOnClickListener(this);

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == GALLERY_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {
            Uri selectedImage = data.getData();
            String[] filePathColumns = {MediaStore.Images.Media.DATA};
            Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePathColumns[0]);
            String imagePath = c.getString(columnIndex);
            Shangchuang(getFileName(imagePath),imagePath);
              httpOkgo(getFileName(imagePath));
            c.close();
        }
    }

    private void httpOkgo(String imagePath) {
        OkGo.<String>get("http://182.254.226.100/hbm/admin/hbmwj.php")
                .params("url",lujin+imagePath)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.d("TAG",response.body());
                    }
                });

    }
    public String getFileName(String a){
        String b = a.substring(a.lastIndexOf("/") + 1, a.length());
        System.out.println(b);
        return b;
    }

    private void Shangchuang(String filename,String filePath) {

        //初始化OssService类，参数分别是Content，accessKeyId，accessKeySecret，endpoint，bucketName（后4个参数是您自己阿里云Oss中参数）
        final OssService ossService = new OssService(this, accessKeyId, accessKeySecret, endpoint, bucketName);
//初始化OSSClient
        ossService.initOSSClient();
//开始上传，参数分别为content，上传的文件名filename，上传的文件路径filePath
        ossService.beginupload(this, filename, filePath);
//上传的进度回调
        ossService.setProgressCallback(new OssService.ProgressCallback() {
            @Override
            public void onProgressCallback(final double progress) {
                Log.d("TAG", "上传进度：" + progress);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                    button.setText("上传中"+getInt(progress)+"%");
                    if (getInt(progress) == 100){
                        button.setText("上传成功");
                    }
                    }
                });
            }

        });

    }
    public static int getInt(double number){
        BigDecimal bd=new BigDecimal(number).setScale(0, BigDecimal.ROUND_HALF_UP);
         return Integer.parseInt(bd.toString());
        }


    @Override
    public void onBackPressed() {
        if (JCVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }
    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_shangchuang:
                Intent intentToPickPic = new Intent(Intent.ACTION_PICK, null);
                // 如果限制上传到服务器的图片类型时可以直接写如："image/jpeg 、 image/png等的类型" 所有类型则写 "image/*"
                intentToPickPic.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/jpeg");
                startActivityForResult(intentToPickPic, GALLERY_REQUEST_CODE);
                break;
            case R.id.bt_fragment:
                break;
            case R.id.bt_lusp:
                break;
        }

    }
}

